package cayte.xiami.in.ui.setting;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cayte.xiami.in.R;
import cayte.xiami.in.ui.TitleView;

import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.UMFeedbackService;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

public class OtherCaptionActivity extends Activity {

	private TextView iVersion;
	private Button iUpdate, iFeedback;
	private TitleView title;
	private Toast toast;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other_caption);

		title = new TitleView(this);
		title.setText(R.string.otherCaption);
		title.setLeftImage(R.drawable.back, new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		iVersion = (TextView) findViewById(R.id.iVersion);
		iUpdate = (Button) findViewById(R.id.iUpdate);
		iFeedback = (Button) findViewById(R.id.iFeedback);

		try {
			PackageManager pm = getPackageManager();
			PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
			iVersion.setText(getString(R.string.iVersion) + pi.versionName);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		iUpdate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showToast(R.string.updateStart);
				update();
			}
		});
		iFeedback.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				UMFeedbackService
						.openUmengFeedbackSDK(OtherCaptionActivity.this);
			}
		});

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private void update() {
		// TODO Auto-generated method stub
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.setUpdateAutoPopup(false);
		UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
			@Override
			public void onUpdateReturned(int updateStatus,
					UpdateResponse updateInfo) {
				switch (updateStatus) {
				case 0: // has update
					if (toast != null)
						toast.cancel();
					UmengUpdateAgent.showUpdateDialog(
							OtherCaptionActivity.this, updateInfo);
					break;
				case 1: // has no update
					showToast(R.string.updateNotUp);
					break;
				case 2: // none wifi
					showToast(R.string.updateNoWifi);
					break;
				case 3: // time out
					showToast(R.string.updateTimeOut);
					break;
				}
			}
		});

		UmengUpdateAgent.update(this);
	}

	private void showToast(int resId) {
		// TODO Auto-generated method stub
		if (toast == null)
			toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
		else
			toast.cancel();
		toast.setText(resId);
		toast.show();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}
}
